﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace NetJsWire.Lite.Editors
{
    public partial class StoreBindingTypeEditorDialog : Form
    {
        private readonly Dictionary<string, object> _assemblys;
        public StoreBindingTypeEditorDialog()
        {
            InitializeComponent();
            _assemblys = new Dictionary<string, object>();
            var assemblys = AppDomain.CurrentDomain.GetAssemblies().Where( a => !a.FullName.StartsWith( "System" ) && !a.FullName.ToLower().StartsWith( "microsoft" ) && !a.FullName.StartsWith( "mscor" ) && !a.FullName.StartsWith( "JetBrains" ) && !a.FullName.StartsWith( "Windows" ) ).ToList();

            foreach ( var assembly in assemblys )
            {
                //    typesList.Items.AddRange( assembly.GetTypes().Where( t => t.IsPublic ).Select( t => t.FullName ).ToArray() );
                var name = assembly.FullName.Substring( 0, assembly.FullName.IndexOf( "," ) );
                if ( !_assemblys.ContainsKey( name ) )
                {
                    var node = new TreeNode( name );
                    //typesList.Items.Add( name );
                    _assemblys.Add( name, assembly );
                    foreach ( var type in assembly.GetTypes().Where( t => t.IsPublic ).OrderBy( t => t.Name ) )
                    {
                        var child = node.Nodes.Add( type.Name );
                        child.Tag = type.FullName;
                    }

                    typesTree.Nodes.Add( node );
                }
            }
            //typesList.DataSource = _assemblys;
        }

        public Type SelectedType { get; set; }

        private void button1_Click( object sender, EventArgs e )
        {
            if ( typesTree.SelectedNode.Tag != null )
            {
                SelectedType = Type.GetType( typesTree.SelectedNode.Tag.ToString() );
                Close();
            }
        }
    }
}
